package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Brand;
import com.ssh.entity.Company;
import com.ssh.entity.SuperMarket;
import com.ssh.manager.CompanyManager;
import com.ssh.manager.SuperMarketManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/superMarket/superMarketList.jsp"),
@Result(name="findSuperMarketByCompanyId", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.content,page\\.pageData\\[\\d+\\]\\.createDate," +
"page\\.pageData\\[\\d+\\]\\.tel,page\\.pageData\\[\\d+\\]\\.company\\.name"}),
@Result(name="deleteSuperMarket", type="json", params={"root","page","excludeproperties","page\\.curPageNum"}),
@Result(name="updateSuperMarket", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="getAllMarketByCid", type="json", params={"root","superMarkets","excludeproperties","superMarkets\\[\\d+\\]\\.id,superMarkets\\[\\d+\\]\\.name"}),
@Result(name="findListByProduct", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.content,page\\.pageData\\[\\d+\\]\\.createDate," +
"page\\.pageData\\[\\d+\\]\\.tel,page\\.pageData\\[\\d+\\]\\.company\\.name"}),
@Result(name="findListByBrand", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
		"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.content,page\\.pageData\\[\\d+\\]\\.createDate," +
		"page\\.pageData\\[\\d+\\]\\.tel,page\\.pageData\\[\\d+\\]\\.company\\.name"})
})
public class SuperMarketAction extends ActionSupport{
	@Resource(name="superMarketManagerImpl")
	private SuperMarketManager superMarketManager;
	@Resource(name="companyManagerImpl")
	private CompanyManager companyManager;
	private JsonResult jr=new JsonResult();
	private SuperMarket superMarket;
	private Company company;
	private Page page = new Page();
	private String isDesc;
	private List<SuperMarket> superMarkets;
	private String brandIds;
	private String ids;
	private Brand brand;
	
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getBrandIds() {
		return brandIds;
	}
	public void setBrandIds(String brandIds) {
		this.brandIds = brandIds;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	public List<SuperMarket> getSuperMarkets() {
		return superMarkets;
	}
	public void setSuperMarkets(List<SuperMarket> superMarkets) {
		this.superMarkets = superMarkets;
	}
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
	public SuperMarket getSuperMarket() {
		return superMarket;
	}
	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}
	
	/**
	 * 根据系统ID分页查询该系统下的门店
	 * @return
	 */
	public String findSuperMarketByCompanyId(){
		//page.setNumberPerPage(20);
		superMarketManager.findSuperMarketsByCID(company.getId(), page, isDesc);
		return "findSuperMarketByCompanyId";
	}
	
	/**
	 * 添加系统门店
	 * @return
	 */
	public String addSuperMarket(){
		company = companyManager.getCompanyById(company.getId());
		superMarket.setDel(0);
		superMarket.setCreateDate(new Timestamp(System.currentTimeMillis()));
		superMarket.setCompany(company);
		superMarket = superMarketManager.addSuperMarket(superMarket);
		return "success";
	}
	
	/**
	 * 修改门店信息
	 * @return
	 */
	public String updateSuperMarket(){
		try {
			SuperMarket superMarket2 = superMarketManager.getSuperMarketById(superMarket.getId());
			superMarket2.setName(superMarket.getName());
			superMarket2.setTel(superMarket.getTel());
			superMarket2.setContent(superMarket.getContent());
			superMarketManager.updateSuperMarket(superMarket2);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("y");
			LogFactory.getDefaultLog().error("系统门店修改失败", LogName.API);
			e.printStackTrace();
		}
		return "updateSuperMarket";
	}
	
	/**
	 * 删除门店
	 * @return
	 */
	public String deleteSuperMarket(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					superMarketManager.delete(new Long(d[i]));
				}
			}else{
				superMarketManager.delete(superMarket.getId());
			}
			
			int _p = (int)page.getCurPageNum();
			page.setNumberPerPage(20);
			page.setCurPageNum(1);
			superMarketManager.findSuperMarketsByCID(company.getId(), page, isDesc);
			if (_p <= page.getTotalPageNum()) {
				page.setCurPageNum(_p);
			}else {
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("系统门店删除失败", LogName.API);
			e.printStackTrace();
		}
		return "updateSuperMarket";
	}
	
	/**
	 * 取出某个公司所有门店
	 * @return
	 */
	public String getAllMarketByCid(){
		superMarkets = superMarketManager.findMarketsByCIDAll(company.getId());
		return "getAllMarketByCid";
	}
	
	/**
	 * 查询所有门店
	 * @return
	 */
	public String findAll(){
		superMarkets = superMarketManager.findAll();
		return "getAllMarketByCid";
	}
	
	public String findListByProduct(){
		String[] _b = brandIds.split(",");
		superMarketManager.findListByBrandIds(_b, page);
		return "findListByProduct";
	}
	
	public String findListByBrand(){
		superMarketManager.findListByBrand(brand.getId(), page);
		return "findListByProduct";
	}
}
